<template>
  <div v-if="visible"
       :style="{zIndex:zIndex}" class="dialog-page">
    <div :style="{
        width:width,
        height:height
      }" class="dialog-page__wrapper">
      <div class="dialog-page__header">
        <slot name="title">
          <span class="el-dialog__title">{{ title }}</span>
        </slot>
        <button
            type="button"
            class="dialog__headerbtn"
            aria-label="Close"
            v-if="showClose"
            title="关闭"
            @click="handleClose">
          <i class="icon-close"></i>
        </button>
      </div>
      <div class="dialog-page__body">
        <slot></slot>
      </div>
      <el-divider v-if="$slots.footer"></el-divider>
      <div v-if="$slots.footer" class="dialog-page__footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script>
let zIndex = 1000
export default {
  name: "Dialog",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    showClose: {
      type: Boolean,
      default: false
    },
    title: String,
    width: {
      type: String,
      default: '50%'
    },
    height: {
      type: String,
      default: 'auto'
    }
  },
  data() {
    return {
      zIndex: zIndex++
    }
  },
  methods: {
    handleClose() {
      this.$emit('close');
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-page {
  background-color: rgba(0, 0, 0, 0.5);
  width: 100vw;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;

  .dialog-page__wrapper {
    margin: 0 auto;
    background-color: white;
    display: flex;
    flex-direction: column;
    border-radius: 5px;
    margin-top: 15vh;
    max-height: 70vh;
    position: relative;
    overflow: hidden;

    .dialog-page__header {
      padding: 20px 20px 10px;

      .dialog__headerbtn {
        height: 54px;
        width: 54px;
        padding: 0;
        border: none;
        background: none;
        position: absolute;
        top: 0;
        right: 0;

        .icon-close {
          height: 100%;
          width: 100%;
          display: block;
          background-position: center;
          background-repeat: no-repeat;
          background-size: 24px;
          background-image: url("~@/assets/svg/close.svg");
        }

        &:hover {
          background-color: #ccc;
        }
      }
    }

    .dialog-page__body {
      flex: 1;
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
      overflow: auto;
    }

    .dialog-page__footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;
    }
  }
}
</style>
